package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.AnnouncementList;
import com.chain.BrotherExchange.data.model.BannerList;
import com.chain.BrotherExchange.data.model.Quotation;
import com.chain.BrotherExchange.data.model.QuotationTypes;
import com.chain.BrotherExchange.data.model.TradingVolume;
import com.chain.BrotherExchange.data.model.UserAssets;
import com.chain.BrotherExchange.utils.Androids;

import java.util.List;

/**
 * 首页
 */
public class HomeViewModel extends AndroidViewModel {

    private final MutableLiveData<Resource<UserAssets.DataBean>> mUserAssertsLiveData;
    private final MutableLiveData<List<BannerList.DataBean.ListBean>> mBannerLiveData;
    private final MutableLiveData<TradingVolume.DataBean> mTradingVolumeMutableLiveData;
    private final MutableLiveData<Resource<List<Quotation.DataBean.ListBean>>> mAmountIncreaseLiveData;
    private final MutableLiveData<List<AnnouncementList.DataBean.ListBean>> mAnnouncementLiveData;

    public HomeViewModel(@NonNull Application application) {
        super(application);
        mBannerLiveData = new MutableLiveData<>();
        mUserAssertsLiveData = new MutableLiveData<>();
        mAnnouncementLiveData = new MutableLiveData<>();
        mAmountIncreaseLiveData = new MutableLiveData<>();
        mTradingVolumeMutableLiveData = new MutableLiveData<>();
    }

    public MutableLiveData<Resource<UserAssets.DataBean>> requestUserAsserts() {
        if (!Androids.LocalConfig.isLogin()) {
            return mUserAssertsLiveData;
        }

        int uid = Androids.LocalConfig.getUid();
        String token = Androids.LocalConfig.getToken();
        mUserAssertsLiveData.setValue(Resource.loading());

        DataRepository.get().userAssets(token, uid, result -> {
            UserAssets.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                mUserAssertsLiveData.setValue(Resource.success(resultData));
            } else if (result.isNotNetwork()) {
                mUserAssertsLiveData.setValue(Resource.notNetwork());
            } else if (result.isLogonFailure()) {
                mUserAssertsLiveData.setValue(Resource.tokenError(result.getMsg()));
            } else {
                mUserAssertsLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mUserAssertsLiveData;
    }

    public MutableLiveData<List<BannerList.DataBean.ListBean>> requestBanner() {
        DataRepository.get().homeBannerList(result -> {
            BannerList.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                List<BannerList.DataBean.ListBean> list = resultData.getList();
                if (list != null && !list.isEmpty()) {
                    mBannerLiveData.setValue(list);
                }
            }
        });

        return mBannerLiveData;
    }

    public MutableLiveData<List<AnnouncementList.DataBean.ListBean>> requestAnnouncement() {
        DataRepository.get().announcementList(result -> {
            AnnouncementList.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                List<AnnouncementList.DataBean.ListBean> list = resultData.getList();
                if (list != null && !list.isEmpty()) {
                    mAnnouncementLiveData.setValue(list);
                }
            }
        });

        return mAnnouncementLiveData;
    }

    public MutableLiveData<TradingVolume.DataBean> requestTradingVolume() {
        DataRepository.get().tradingVolume(result -> {
            if (result.isSucceed() && result.getData() != null) {
                mTradingVolumeMutableLiveData.setValue(result.getData());
            }
        });

        return mTradingVolumeMutableLiveData;
    }

    public MutableLiveData<Resource<List<Quotation.DataBean.ListBean>>> requestAmountIncrease() {
        mAmountIncreaseLiveData.setValue(Resource.loading());

        DataRepository.get().quotationType(data -> {
            if(data.isSucceed()){
                QuotationTypes.DataBean typeData = data.getData();

                if(typeData != null && !typeData.getList().isEmpty()){
                    int type = typeData.getList().get(0).getToken_id();
                    DataRepository.get().quotationInfo(type, result -> {

                        Quotation.DataBean resultData = result.getData();
                        if (result.isSucceed() && resultData != null) {
                            List<Quotation.DataBean.ListBean> list = resultData.getList();
                            if (list != null && !list.isEmpty()) {
                                mAmountIncreaseLiveData.setValue(Resource.success(list));
                            } else {
                                mAmountIncreaseLiveData.setValue(Resource.empty());
                            }

                        } else if (result.isNotNetwork()) {
                            mAmountIncreaseLiveData.setValue(Resource.notNetwork());

                        } else {
                            mAmountIncreaseLiveData.setValue(Resource.error(result.getMsg()));
                        }
                    });
                }
            }else if (data.isNotNetwork()) {
                mAmountIncreaseLiveData.setValue(Resource.notNetwork());

            } else {
                mAmountIncreaseLiveData.setValue(Resource.error(data.getMsg()));
            }
        });



        return mAmountIncreaseLiveData;
    }
}
